package com.example.controller;

import com.example.pojo.Videos;
import com.example.service.VideosService;
import com.example.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/videos")
public class VideosController {
    @Autowired
    private VideosService videosService;
    // 获取所有视频

    @GetMapping("/all")
    public List<Videos> getAllVideos() {
        return videosService.findAll();
    }
    // 根据id获取视频

    @GetMapping("/videos/{id}")
    public Videos getVideoById(@PathVariable("id") int id) {
        return videosService.findById(id);
    }

    // 删除视频
    @DeleteMapping("/delete/{id}")
    public void deleteVideo(@PathVariable Integer id) {
        videosService.delete(id);
    }
    // 增加视频
    @PostMapping("/add")
    public void addVideo(@RequestParam("url") MultipartFile url,
                         @RequestParam("title") String title,
                         @RequestParam("description") String description,
                         @RequestParam("regionName") String regionName,
                         @RequestParam("speciesName") String speciesName) {
        Videos video = new Videos();
        video.setTitle(title);
        video.setDescription(description);
        video.setRegionName(regionName);
        video.setSpeciesName(speciesName);
        String Path = null;
        try {
            Path = FileUploadUtils.processFileUpload(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        video.setUrl(Path);
        videosService.insert(video);
    }
    // 更新视频
    @PutMapping("/update/{id}")
    public void updateVideo(@PathVariable Integer id,
                              @RequestParam(value = "url", required = false) MultipartFile url,
                              @RequestParam(value = "title", required = false) String title,
                              @RequestParam(value = "description", required = false) String description,
                              @RequestParam(value = "regionName", required = false) String regionName,
                              @RequestParam(value = "speciesName",required = false) String speciesName) {
        Videos video = videosService.findById(id);
        if (title != null) {
            video.setTitle(title);
        }
        if (description != null) {
            video.setDescription(description);
        }
        if (regionName != null) {
            video.setRegionName(regionName);
        }
        if (speciesName != null) {
            video.setSpeciesName(speciesName);
        }
        if (url != null && !url.isEmpty()) {
            String Path = null;
            try {
                Path = FileUploadUtils.processFileUpload(url);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            video.setUrl(Path);
        }
        videosService.update(video);
    }
}
